import pickle
from multiprocessing import Pool
import os
import scipy
from tqdm import tqdm
import mat73
import json



# class Func4MultiProcessing: # 把多线程函数的逻辑部分打包进这个class，方便统一接口。
#     def zuco2eeg(self, zuco2path, task_name, sbj_name, tn):
#         file_name = f"{zuco2path}/{task_name}/Matlab files/results{sbj_name}_{tn}.mat"
#         print(f"---==Reading[{file_name}]==---")
#         text_ = mat73.loadmat(file_name)['sentenceData']['content']



#         json.dump(text_, open(f"./tmp/{sbj_name}_{tn}.json", "w", encoding="utf-8"), ensure_ascii=False,indent=4)
#         print(f"---==Save [./tmp/{sbj_name}_{tn}.json]==---")

#     @staticmethod
#     def error_back(eb):
#         print(f'error: {str(eb)}')


# def mutipro_start(func, zuco2path, task_name, sbj_names_, tn):
#     working_thread = []
#     workernum = len(sbj_names_)
#     with Pool(processes=workernum) as pool:
#         for sbj_name in sbj_names_:
#             w = pool.apply_async(
#                 func=func.zuco2eeg,
#                 args=(zuco2path, task_name, sbj_name, tn),
#                 error_callback=func.error_back
#             )
#             working_thread.append(w)
#         for _ in range(workernum):
#             working_thread[_].get()


# def data_prepare_zuco2(zuco2path="./rawdata/zuco2", workernum=4):
#     for task_name in os.listdir(zuco2path):
#         if "NR" in task_name:
#             tn = "NR"
#         elif "TSR" in task_name:
#             tn = "TSR"
#         else:
#             continue
#         sbj_names = list(os.listdir(f"{zuco2path}/{task_name}/Raw data"))
#         for i in range(0, len(sbj_names), workernum):
#             sbj_names_ = sbj_names[i:i + workernum]

#             func = Func4MultiProcessing()
#             mutipro_start(func, zuco2path, task_name, sbj_names_, tn)

#     print("aaa")

# data_prepare_zuco2()

NRli, TSRli = [], []
for file in os.listdir("./tmp/"):
    if "NR" in file:
        NRli.append(file)
    elif "TSR" in file:
        TSRli.append(file)

NR0 = json.load(open(f"./tmp/{NRli[0]}", "r", encoding="utf-8"))

for i in NRli[1:]:
    NRtmp = json.load(open(f"./tmp/{i}", "r", encoding="utf-8"))
    if NR0 != NRtmp:
        print("NR not equal")
        print(i)

TSR0 = json.load(open(f"./tmp/{TSRli[0]}", "r", encoding="utf-8"))
for i in TSRli[1:]:
    TSRtmp = json.load(open(f"./tmp/{i}", "r", encoding="utf-8"))
    if TSR0 != TSRtmp:
        print("TSR not equal")
        print(i)
